<?php
include_once('common.php');
include_once 'meekrodb.class.php';
DB::$host = 'localhost'; //defaults to localhost if omitted
DB::$port = '3306'; // defaults to 3306 if omitted
DB::$encoding = 'utf8'; // defaults to latin1 if omitted
DB::$dbName = 'ltrvani';
DB::$user = 'root';
DB::$password = '';

function getChannelIdListByDay($day, $type){

	$sql = "SELECT l.`id`, l.`name`, l.`type`, l.`url`, l.`url1`, l.`url2`"
	." \nFROM `l_channel` l INNER JOIN `l_channel_schedule` s ON l.id = s.channel_id"
	." \nWHERE s.day = %d ";
	if($type){
		$sql .= ' AND l.`type` = %s';
	}

	$channelList= DB::query($sql, $day, $type);

	$result=array();
	foreach ($channelList as $channel){
		$result[]=$channel["id"];
	}

	return $result;
}


function getChannelResultByDate($channel_id, $open_date){

	$sql = "SELECT * "
	." \nFROM `l_channel_result`"
	." \nWHERE channel_id = %d AND open_date=%s ";

	return DB::query($sql, $channel_id, $open_date->format("Y-m-d"));
}

function updateTicket($transaction, $is_win,$factor){
	$transaction["status"]=$is_win?'WIN':'LOSE';
	$transaction["win_lose"]=$is_win?$transaction["stake"]*$transaction["odds"]*$factor:0;
	$transaction["commission"]=$transaction["stake"]*$transaction["commission_rate"];
	$transaction["agent_win_lose"]=$transaction["agent_rate"]*($transaction["stake"]-$transaction["win_lose"]);
	$transaction["affiliate_win_lose"]=$transaction["affiliate_rate"]*($transaction["stake"]-$transaction["win_lose"]);
	$transaction["affiliate_commission"]=$transaction["affiliate_commission_rate"]*$transaction["stake"];
	$sql = "UPDATE `l_transaction`"
	." \nSET `status` = %s ,"
	." win_lose = %d ,"
	." commission = %d ,"
	." agent_win_lose = %d ,"
	." affiliate_win_lose = %d ,"
	." affiliate_commission = %d "
	." \nWHERE `id`= %d";

	DB::query($sql,
	$transaction["status"],
	$transaction["win_lose"],
	$transaction["commission"],
	$transaction["agent_win_lose"],
	$transaction["affiliate_win_lose"],
	$transaction["affiliate_commission"],
	$transaction["id"]);
}

function trungLo($resultList , $choice){
	$win_count=0;
	foreach($resultList as $result){
		if(endsWith($result["result"], $choice)){
			$win_count++;
		}
	}

	return $win_count;
}


function settleTicket($resultList, $transaction){
	$result_count= sizeof($resultList);
	switch ($transaction["trans_type"]) {

		case 'MN_X5.3': // BAO LÔ 2 SỐ: X5.3
		case 'MN_X45': // BAO LÔ 3 SỐ: X45
		case 'MN_X450': // BAO LÔ 4 SỐ: X450
		case 'MT_X5.3': // BAO LÔ 2 SỐ: X5.3
		case 'MT_X45': // BAO LÔ 3 SỐ: X45
		case 'MT_X450': // BAO LÔ 4 SỐ: X450
		case 'MB_X3.5': // BAO LÔ 2 SỐ: x3.5
		case 'MB_X34': // BAO LÔ 3 SỐ: X34
		case 'MB_X400': // BAO LÔ 4 SỐ: x400
			{
				$win_count=trungLo($resultList, $transaction["choice"]);
				updateTicket($transaction,$win_count>0,$win_count);
			}
			break;
		case 'MN_X30': // LÔ XIÊN 2: X30
		case 'MT_X30': // LÔ XIÊN 2: X30
		case 'MB_X12': // LÔ XIÊN 2: X12
			{
				$pieces = explode("-", $transaction["choice"]);
				$xo_lien_1=$pieces[0];
				$xo_lien_2=$pieces[1];
				$win_1=trungLo($resultList, $xo_lien_1);
				$win_2=trungLo($resultList, $xo_lien_2);
				$is_win=$win_1>0&&$win_2>0;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_X150': // LÔ XIÊN 3: X150
		case 'MT_X150': // LÔ XIÊN 3: X150
		case 'MB_X45': // LÔ XIÊN 3: X45
			{
				$pieces = explode("-", $transaction["choice"]);
				$xo_lien_1=$pieces[0];
				$xo_lien_2=$pieces[1];
				$xo_lien_3=$pieces[2];
				$win_1=trungLo($resultList, $xo_lien_1);
				$win_2=trungLo($resultList, $xo_lien_2);
				$win_3=trungLo($resultList, $xo_lien_3);
				$is_win=$win_1>0&&$win_2>0&&$win_3>0;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_X750': // LÔ XIÊN 4: X750
		case 'MT_X750': // LÔ XIÊN 4: X750
		case 'MB_X150': // LÔ XIÊN 4: X150
			{
				$pieces = explode("-", $transaction["choice"]);
				$xo_lien_1=$pieces[0];
				$xo_lien_2=$pieces[1];
				$xo_lien_3=$pieces[2];
				$xo_lien_4=$pieces[4];
				$win_1=trungLo($resultList, $xo_lien_1);
				$win_2=trungLo($resultList, $xo_lien_2);
				$win_3=trungLo($resultList, $xo_lien_3);
				$win_4=trungLo($resultList, $xo_lien_4);
				$is_win=$win_1>0&&$win_2>0&&$win_3>0&&$win_4>0;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_H_X800': // XỈU CHỦ ĐẦU: X800
		case 'MT_H_X800': // XỈU CHỦ ĐẦU: X800
			{
				$is_win=endsWith($resultList[$result_count-2]["result"],$transaction["choice"]);
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_T_X800': // XỈU CHỦ ĐUÔI: X800
		case 'MT_T_X800': // XỈU CHỦ ĐUÔI: X800
		case 'MB_X800': // 3 CÀNG : x800
			{
				$is_win=endsWith($resultList[0]["result"],$transaction["choice"]);
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_X400': // XỈU CHỦ ĐẦU ĐUÔI: X400
		case 'MT_X400': // XỈU CHỦ ĐẦU ĐUÔI: X400
			{
				$is_winHead=endsWith($resultList[$result_count-2]["result"],$transaction["choice"]);
				$is_winTail=endsWith($resultList[0]["result"],$transaction["choice"]);
				$is_win = $is_winHead&& $is_winTail;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_H_X8.5': // ĐÁNH ĐẦU: X8.5
		case 'MT_H_X8.5': // ĐÁNH ĐẦU: X8.5
			{
				$dacbiet= (int)$resultList[0]["result"];
				$head = ($dacbiet%100)/10;
				$is_win = (int)$transaction["choice"] ===(int)$head;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_T_X8.5': // ĐÁNH ĐUÔI: X8.5
		case 'MT_T_X8.5': // ĐÁNH ĐUÔI: X8.5
			{
				$dacbiet= (int)$resultList[0]["result"];
				$tail = $dacbiet%10;
				$is_win = (int)$transaction["choice"] ===(int) $tail;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_H_X85': // ĐỀ ĐẦU: X85
		case 'MT_H_X85': // ĐỀ ĐẦU: X85
			{
				$is_win=endsWith($resultList[$result_count-1]["result"],$transaction["choice"]);
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_T_X85': // ĐỀ ĐUÔI: X85
		case 'MT_T_X85': // ĐỀ ĐUÔI: X85
		case 'MB_X85': // ĐỀ: X85
			{
				$is_win=endsWith($resultList[0]["result"],$transaction["choice"]);
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_TXH_X1.9': // TÀI XỈU ĐẦU: X1.9
		case 'MT_TXH_X1.9': // TÀI XỈU ĐẦU: X1.9
			{
				$head= (int)$resultList[$result_count-1]["result"];
				$head=$head%10;
				$is_win = ($head>=5 && $transaction["choice"]=="TAI") || ($head<5 && $transaction["choice"]=="XIU");
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_TXT_X1.9': // TÀI XỈU ĐUÔI: X1.9
		case 'MT_TXT_X1.9': // TÀI XỈU ĐUÔI: X1.9
		case 'MB_TX_X1.9': // TÀI XỈU: X1.9
			{
				$tail= (int)$resultList[0]["result"];
				$tail=$tail%10;
				$is_win = ($tail>=5 && $transaction["choice"]=="TAI") || ($tail<5 && $transaction["choice"]=="XIU");
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_CLH_X1.9': // CHẴN LẺ ĐẦU: X1.9
		case 'MT_CLH_X1.9': // CHẴN LẺ ĐẦU: X1.9
			{
				$head= (int)$resultList[$result_count-1]["result"];
				$is_win = ($head%2==0 && $transaction["choice"]=="CHAN") ||($head%2==1 && $transaction["choice"]=="LE") ;
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MN_CLT_X1.9': // CHẴN LẺ ĐUÔI: X1.9
		case 'MT_CLT_X1.9': // CHẴN LẺ ĐUÔI: X1.9
		case 'MB_CL_X1.9': // CHẴN LẺ: X1.9
			{
				$tail= (int)$resultList[0]["result"];
				$is_win = ($tail%2==0 && $transaction["choice"]=="CHAN") || ($tail%2==1 && $transaction["choice"]=="LE");
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MB_H_X8.5': // ĐÁNH ĐỀ ĐẦU: X8.5
			{
				$tail= (int)$resultList[0]["result"];
				$is_win = $tail / 10 == $transaction["choice"];
				updateTicket($transaction,$is_win,1);
			}
			break;
		case 'MB_T_X8.5': // ĐÁNH ĐỀ ĐUÔI: X8.5
			{
				$tail= (int)$resultList[0]["result"];
				$is_win = $tail %10 == $transaction["choice"];
				updateTicket($transaction,$is_win,1);
			}
			break;

	}
}

function getTicketByChannelDate($channel_id, $date){

	$sql = "SELECT *"
	." \nFROM `l_transaction`"
	." \nWHERE channel_id = %d AND settle_date = %s";

	return DB::query($sql, $channel_id, $date->format("Y-m-d"));
}

function calculateWinLose($channel_id, $settle_date){
	$sql = "SELECT SUM(t.win_lose - t.stake + t.commission) AS member_win_lose , "
	." \nSUM(t.agent_win_lose) AS agent_win_lose, "
	." \nSUM(t.affiliate_win_lose + t.affiliate_commission ) AS affiliate_win_lose, "
	." \nSUM(t.stake - t.win_lose - t.commission - t.agent_win_lose - t.affiliate_win_lose - t.affiliate_commission) as host_win_lose "
	." \nFROM `l_transaction` t INNER JOIN l_user u ON t.user_id= u.user_id "
	." \nWHERE t.settle_date = %s "
	." \nAND t.channel_id = %d "
	." \nAND u.user_type ='USER' "
	." \nAND (t.status = 'WIN' or t.status= 'LOSE')";

	return DB::query($sql,$settle_date->format("Y-m-d"), $channel_id);
}

function getChannelName($id){
	$sql = "SELECT *"
	." \nFROM `l_channel`"
	." \nWHERE id = %d";

	$channelList =DB::query($sql, $id);
	return sizeof($channelList)>0?$channelList[0]["name"]:"";
}
?>